import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import PlainScore from '../components/SZG/PlainScore/PlainScore.vue'
import ChongZhi from '../components/SZG/ChongZhi/ChongZhi.vue'
import CZSuccess from '../components/SZG/ChongZhi/CZSuccess.vue'
import CZFail from '../components/SZG/ChongZhi/CZFail.vue'
import event from '../components/JC/staggerer/staggerer.vue'
import Login from '../components/SZG/Login.vue'

Vue.use(Router);
let router = new Router({
  // mode:"history",
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/plain',
      name: 'plain',
      component: PlainScore
    },
    {
      path: '/cz',
      name: 'cz',
      component: ChongZhi
    },
    {
      path: '/czsu/:count/:number',// 消费都比  排队序号
      name: 'czsu',
      component: CZSuccess
    }
    ,
    {
      path: '/czfa',
      name: 'czfa',
      component: CZFail
    }
    ,
    {
      path: '/event',
      name: 'event',
      component: event
    }
    , {
      path: "/login",
      name: "login",
      component: Login
    }
  ]
})
if (process.env.NODE_ENV === 'production') {
  config.isProduct = true;

} else {
  config.isProduct = false;
}
router.beforeEach((to, from, next) => {
  if (config.isProduct) {
    //有2种code 地址栏内和Storage内
    let code = DYD.getCode();
    if (!code) {
      window.location.href = config.szg.start;
    }
    let codeFromStorage = Storage.get('urlCode');
    if (code == codeFromStorage) {
      next();
      return;
    }
    Storage.set('urlCode', code);
    if (config.jc.info.baseToken) {
      next();
      return;
    }
    //
    console.log(config.jc.info.openid());
    ajaxCore.ajax(config.jc.getOpenidByCode, {code: code}, "GET", null).done((data) => {
      if (data.status == 10001) {
        //未绑定
        config.jc.info.access_token = data.result.access_token;
        Storage.set("access_token", config.jc.info.access_token);
        Storage.set("openid", data.result.openid);
        DYD.msg("获取微信信息成功");
        next('/login')
      } else if (data.status == 10101) {
        //已经绑定
        config.jc.info.Token = data.result.token;
        config.jc.info.baseToken = BASE64.encode("token:" + data.result.token);
        config.jc.info.nickName = data.result.nickname;
        config.jc.info.nickImg = data.result.imgurl;
        config.jc.info.jf = data.result.jf;
        config.jc.info.flag = data.result.flag;
        Storage.set("openid", data.result.openid);
        Storage.set("Token", config.jc.info.Token);
        Storage.set("baseToken", config.jc.info.baseToken);
        Storage.set("nickName", config.jc.info.nickName);
        Storage.set("nickImg", config.jc.info.nickImg);
        Storage.set("jf", config.jc.info.jf);
        Storage.set("flag", config.jc.info.flag);
        DYD.msg("登录成功");
        DYD.getTypeByUrl(window.location.href, 1)
        if (to.name == 'login') {
          next('/plain')
        }
      } else if (data.status == 10002) {
        //增加跳转回登录页面
        next('/login')
        DYD.msg(data.desc)
      } else {
        DYD.msg("获取微信信息失败");
      }
    });
  } else {
    next()
  }

})
export default router
